/*-----------------------------------------------------------------------*/
/* Low level disk I/O module SKELETON for FatFs     (C)ChaN, 2019        */
/*-----------------------------------------------------------------------*/
/* If a working storage control module is available, it should be        */
/* attached to the FatFs via a glue function rather than modifying it.   */
/* This is an example of glue functions to attach various exsisting      */
/* storage control modules to the FatFs module with a defined API.       */
/*-----------------------------------------------------------------------*/

#include "ff.h"			/* Obtains integer types */
#include "diskio.h"		/* Declarations of disk functions */
#include "main.h"

/*-----------------------------------------------------------------------*/
/* Get Drive Status                                                      */
/*-----------------------------------------------------------------------*/

DSTATUS disk_status (
	BYTE pdrv		/* Physical drive nmuber to identify the drive */
)
{
	return RES_OK;
}

/*-----------------------------------------------------------------------*/
/* Inidialize a Drive                                                    */
/*-----------------------------------------------------------------------*/

DSTATUS disk_initialize (
	BYTE pdrv				/* Physical drive nmuber to identify the drive */
)
{
	return RES_OK;
}

/*-----------------------------------------------------------------------*/
/* Read Sector(s)                                                        */
/*-----------------------------------------------------------------------*/
extern SD_HandleTypeDef hsd1;

DRESULT disk_read (
	BYTE pdrv,		/* Physical drive nmuber to identify the drive */
	BYTE *buff,		/* Data buffer to store read data */
	LBA_t sector,	/* Start sector in LBA */
	UINT count		/* Number of sectors to read */
)
{
	while (HAL_SD_GetCardState(&hsd1) != 4);
	__asm volatile("cpsid i");
	HAL_SD_ReadBlocks(&hsd1, buff, sector, count, HAL_MAX_DELAY);
	__asm volatile("cpsie i");

	return RES_OK;
}

/*-----------------------------------------------------------------------*/
/* Write Sector(s)                                                       */
/*-----------------------------------------------------------------------*/

#if FF_FS_READONLY == 0

DRESULT disk_write (
	BYTE pdrv,			/* Physical drive nmuber to identify the drive */
	const BYTE *buff,	/* Data to be written */
	LBA_t sector,		/* Start sector in LBA */
	UINT count			/* Number of sectors to write */
)
{
	__asm volatile("cpsid i");  // 关闭中断
	while (HAL_SD_GetCardState(&hsd1) != 4);
	HAL_SD_WriteBlocks(&hsd1, buff, sector, count, 100);
	__asm volatile("cpsie i");  // 开启中断
	
	return RES_OK;
}

#endif


/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions                                               */
/*-----------------------------------------------------------------------*/

DRESULT disk_ioctl (
	BYTE pdrv,		/* Physical drive nmuber (0..) */
	BYTE cmd,		/* Control code */
	void *buff		/* Buffer to send/receive control data */
)
{
	DRESULT res = RES_PARERR;
	switch (cmd) 
	{ 
		case GET_SECTOR_SIZE:   // 扇区大小
			*(DWORD *)buff = 512; 
			res = RES_OK; 
			break; 

		case GET_BLOCK_SIZE:   // 块大小
			*(WORD *)buff = hsd1.SdCard.LogBlockSize; 
			res = RES_OK; 
			break; 

		case GET_SECTOR_COUNT:   // 扇区数量
			*(DWORD *)buff = hsd1.SdCard.LogBlockNbr; 
			res = RES_OK; 
			break; 
	}
	return res;
}

DWORD get_fattime (void)
{
	DWORD tm_year = 2024, tm_mon = 6, tm_day = 21, tm_hour = 0, tm_min = 0, tm_sec = 0;
	return (DWORD)(tm_year - 80) << 25 |
           (DWORD)(tm_mon + 1) << 21 |
           (DWORD)tm_day << 16 |
           (DWORD)tm_hour << 11 |
           (DWORD)tm_min << 5 |
           (DWORD)tm_sec >> 1;
}

